from downloadmoviethread import download_video_m3u8
from search_movie import search_movie_by_name,get_movie_videos,get_video_vid,get_m3u8_url

if __name__ == "__main__":
    name = input("请输入电影名称:")
    url_dict = search_movie_by_name(name)
    if not url_dict:
        exit(0)
    for filename in url_dict.keys():
        print("电影名:{}".format(filename))
    filename = input("请输入你选择的电影:")
    filename_url = url_dict.get(filename)
    print(filename_url)
    if not filename_url:
        exit(0)
    detail_url_list = get_movie_videos(filename_url)
    print(detail_url_list)
    for server_id in range(len(detail_url_list)):
        print("视频服务器选择:{}号".format(server_id+1))
    server_id = int(input("请输入你选择几号服务器:(数字)"))
    if server_id-1 >len(detail_url_list) or server_id<0:
        exit(0)
    video_detail_url = detail_url_list[server_id-1]
    print("*"*10)
    for video_name in video_detail_url.keys():
        print(video_name)
    video_name = input("请选择你要看的集数:(copy 想看的即可)")
    if not video_detail_url.get(video_name):
        exit(0)
    video_url = video_detail_url.get(video_name)
    vid = get_video_vid(video_url)
    if not len(vid):
        exit(1)
    m3u8_url,video_type = get_m3u8_url(vid)
    print(m3u8_url)
    if video_type == "m3u8":
        code = download_video_m3u8(url=m3u8_url,name=str.replace(filename+video_name,"/",""))
        print("访问出现:{}".format(code))

    elif not video_type:
        print("资源不存在")
    elif video_type == "link":
        from player import get_player
        get_player(link_url=m3u8_url)
    else :
        print("暂未处理该类型视频:{},请更换视频服务器".format(video_type))
        raise Exception("暂未处理该类型视频:{},请更换视频服务器".format(video_type))
